<?php

namespace addons\shopro\controller;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\Withdraw as WithdrawModel;
use addons\shopro\service\Withdraw as WithdrawLibrary;
use app\common\model\GoldenLog;
use app\common\model\shopro\WithdrawGoldenModel;
use app\common\model\User;
use app\common\model\Userchildren;
use think\Db;
use app\common\library\Ems;
class Withdraw extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $user      = auth_user();
        $withdraws = WithdrawModel::where(['user_id' => $user->id])->order('id desc')->paginate($this->request->param('list_rows',
            10))->each(function ($withdraw) {
            $withdraw->hidden(['withdraw_info']);
        });

        $this->success('获取成功', $withdraws);
    }

    public function golend_log(){
         $user      = auth_user();
        $withdraws = WithdrawGoldenModel::where(['user_id' => $user->id])->order('id desc')->paginate($this->request->param('list_rows',
            10))->each(function ($withdraw) {
        });

        $this->success('获取成功', $withdraws);
    }

    // 提现规则
    public function rules()
    {
        $user   = auth_user();
        $config = (new WithdrawLibrary($user))->config;

        $this->success('提现规则', $config);
    }


    // 发起提现请求
    public function apply()
    {
        $user   = auth_user();
        if($user->is_frozen == 1 || $user->is_lock == 1){
            $this->error('账户已被冻结，请联系工作人员');
        }
        $params = $this->request->param();
        $this->svalidate($params, ".apply");
        $email = input('email');
        $code  = input('code');
        if(empty($params['secure_password'])){
            $this->error('请输入支付密码');
        }
        if($this->auth->getEncryptPassword($params['secure_password'], $user->salt) != $this->auth->secure_password){
            $this->error('支付密码错误');
        }
//        if(!$email || !$code){
////            $this->error('请填写邮箱和验证码');
//        }
//        $params['firepower'] = 0;
//        if($user->is_firepower == 1){
//            $firepower = sheep_config('shop.recharge_withdraw.withdraw.firepower');
//            if($firepower > 0){
//                $params['firepower'] = bcmul($params['money'],$firepower/100,2);
//                if($user->firepower < $params['firepower']){
//                    $this->error('火力值不够');
//                }
//            }
//        }


        $withdrawLib = new WithdrawLibrary($user);
        $withdraw    = $withdrawLib->apply($params);

        $this->success('申请成功', $withdraw);
    }

    /**
     * @menu:绿色积分兑换黄金券
     * @
     * @Date:2024-04-14 18:54
     */
    public function green_exchange()
    {
        $user   = auth_user();
        $money = input('post.money', 0);
        $type  = input('type',1);
        if ($money <= 0) {
            $this->error('请输入数量');
        }
        if(!in_array($type, ['golden','principal'])){
            $this->error('参数错误');
        }
        if($type == 'golden'){
            $sxf        = bcmul($money, config('site.green_duihuan'));
            $memo = '兑换黄金券';
        }else{
            $sxf        = bcmul($money, config('site.green_zjjf'));
            $memo = '转增金积分';
        }
        if ($user->score < $sxf) {
            $this->error('你没有那么多绿色积分');
        }
        $withdraw = Db::transaction(function () use ($money, $sxf,$user,$type,$memo) {
             WalletService::change($user->id, 'score', -$money, 'score_exchange', [],
                 $memo);
           return WalletService::change($user->id, $type, $sxf, $type.'_exchange', [],
               $memo);
        });
        if($withdraw){
            $this->success('兑换成功');
        }
        $this->error('兑换失败');
    }

    /**
     * @menu:黄金券转让
     * @
     * @Date:2024-04-14 21:41
     */

//    public function golden_turn(){
//        $config = sheep_config('shop.recharge_withdraw.withdraw');
//        if($config['auto_arrival'] == 1){
//            $this->error('暂无法转账');
//        }
//        $mobile = input('post.mobile');
//        $secure_password    = input('secure_password');
//        if(!$mobile || !$secure_password){
//            $this->error('缺少参数');
//        }
//        if($this->auth->secure_password != $this->auth->getEncryptPassword($secure_password, $this->auth->salt)){
//            $this->error('安全密码不对');
//        }
//        $user   = auth_user();
//        $money = input('post.money', 0);
//        if ($money <= 0) {
//            $this->error('请输入正确黄金券');
//        }
//        $twoUserInfo = User::where(['mobile'=>$mobile])->find();
//        if(!$twoUserInfo){
//            $this->error('用户不存在');
//        }
//        $userchildrenModel = new Userchildren();
//        $teshu = $userchildrenModel->where(['member_id'=>761,'children_id'=>$this->auth->id])->find();
//        if($teshu){
//             if($twoUserInfo['id'] =! 761){
//                 $this->error('不能个该用户转');
//             }
//        }else{
//            if($this->auth->is_manager  == 1){
//                $where = ['member_id'=>$this->auth->id,'children_id'=>$twoUserInfo['id']];
//            }else{
//                if($this->auth->mobile == $mobile){
//                    $this->error('不能给自己转');
//                }
//                $where = ['member_id'=>$twoUserInfo['id'],'children_id'=>$this->auth->id];
//                if($twoUserInfo['is_manager'] !=1){
//                    $this->error('不能给该用户转');
//                }
//            }
//            $userchildren = $userchildrenModel->where($where)->find();
//            if(!$userchildren){
//                $this->error('不能为该用户转');
//            }
//        }
//        if ($user->golden < $money) {
//            $this->error('你没有那么多黄金券可提现');
//        }
//        $withdraw = Db::transaction(function () use ($money,$user,$twoUserInfo) {
//
//            $withdraw =  WalletService::change($user->id, 'golden', -$money, 'golden_turn', [],
//                '黄金券互转');//扣除黄金券
//            $withdraw =  WalletService::change($twoUserInfo->id, 'gold', $money, 'gold_turn', [],
//                '黄金券互转');//增加金币
//            return $withdraw;
//        });
//        if($withdraw){
//            $this->success('转让成功');
//        }
//        $this->error('转让失败');
//    }

    /**
     * @menu 金币互转
     * @
     * @Date:2024-04-22 13:58
     */
//    public function gold_turn(){
////        $config = sheep_config('shop.recharge_withdraw.withdraw');
////        if($config['auto_arrival'] == 1){
////            $this->error('暂无法转账');
////        }
//        $mobile = input('post.mobile');
//        $secure_password    = input('secure_password');
//        if(!$mobile || !$secure_password){
//            $this->error('缺少参数');
//        }
//
//        $user   = auth_user();
//        if($user->secure_password != $this->auth->getEncryptPassword($secure_password, $user->salt)){
//            $this->error('安全密码不对');
//        }
//        $money = input('post.money', 0);
//        if ($money <= 0) {
//            $this->error('请输入正确金币');
//        }
//        if($user->gold < $money){
//            $this->error('可转金币不足');
//        }
//        $twoUserInfo = User::where(['id'=>['neq',$user->id],'mobile'=>$mobile])->find();
//        if(!$twoUserInfo){
//            $this->error('用户不存在');
//        }
//
//        $withdraw = Db::transaction(function () use ($money,$user,$twoUserInfo) {
//            $withdraw =  WalletService::change($user->id, 'gold', -$money, 'gold_turn', [],
//                '用户'.$user->id.'金币赠予用户'.$twoUserInfo->id);//扣除金币
//            $withdraw =  WalletService::change($twoUserInfo->id, 'gold', $money, 'gold_receive', [],
//                '用户'.$user->id.'金币赠予用户'.$twoUserInfo->id);//增加金币
//            return $withdraw;
//        });
//        if($withdraw){
//            $this->success('转赠成功');
//        }
//        $this->error('转让失败');
//    }

}
